home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / non-ANSI / c-client / news.c < prev    next >
C/C++ Source or Header  |  1996-05-15  |  35KB  |  1,316 lines

  1. /*
  2.  * Program:    Netnews mail routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    4 September 1991
  13.  * Last Edited:    15 May 1996
  14.  *
  15.  * Copyright 1996 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include <errno.h>
  39. extern int errno;        /* just in case */
  40. #include "mail.h"
  41. #include "osdep.h"
  42. #include <sys/stat.h>
  43. #include <sys/time.h>
  44. #include "news.h"
  45. #include "rfc822.h"
  46. #include "misc.h"
  47. #include "newsrc.h"
  48.  
  49. /* Netnews mail routines */
  50.  
  51.  
  52. /* Driver dispatch used by MAIL */
  53.  
  54. DRIVER newsdriver = {
  55.   "news",            /* driver name */
  56.   (DRIVER *) NIL,        /* next driver */
  57.   news_valid,            /* mailbox is valid for us */
  58.   news_parameters,        /* manipulate parameters */
  59.   news_find,            /* find mailboxes */
  60.   news_find_bboards,        /* find bboards */
  61.   news_find_all,        /* find all mailboxes */
  62.   news_find_all_bboards,    /* find all bboards */
  63.   news_subscribe,        /* subscribe to mailbox */
  64.   news_unsubscribe,        /* unsubscribe from mailbox */
  65.   news_subscribe_bboard,    /* subscribe to bboard */
  66.   news_unsubscribe_bboard,    /* unsubscribe from bboard */
  67.   news_create,            /* create mailbox */
  68.   news_delete,            /* delete mailbox */
  69.   news_rename,            /* rename mailbox */
  70.   news_open,            /* open mailbox */
  71.   news_close,            /* close mailbox */
  72.   news_fetchfast,        /* fetch message "fast" attributes */
  73.   news_fetchflags,        /* fetch message flags */
  74.   news_fetchstructure,        /* fetch message envelopes */
  75.   news_fetchheader,        /* fetch message header only */
  76.   news_fetchtext,        /* fetch message body only */
  77.   news_fetchbody,        /* fetch message body section */
  78.   news_setflag,            /* set message flag */
  79.   news_clearflag,        /* clear message flag */
  80.   news_search,            /* search for message based on criteria */
  81.   news_ping,            /* ping mailbox to see if still alive */
  82.   news_check,            /* check for new messages */
  83.   news_expunge,            /* expunge deleted messages */
  84.   news_copy,            /* copy messages to another mailbox */
  85.   news_move,            /* move messages to another mailbox */
  86.   news_append,            /* append string message to mailbox */
  87.   news_gc            /* garbage collect stream */
  88. };
  89.  
  90.                 /* prototype stream */
  91. MAILSTREAM newsproto = {&newsdriver};
  92.  
  93. /* Netnews mail validate mailbox
  94.  * Accepts: mailbox name
  95.  * Returns: our driver if name is valid, NIL otherwise
  96.  */
  97.  
  98. DRIVER *news_valid (name)
  99.     char *name;
  100. {
  101.   int fd;
  102.   char *s,*t,*u;
  103.   char tmp[MAILTMPLEN];
  104.   struct stat sbuf;
  105.   DRIVER *ret = NIL;
  106.                 /* looks plausible and news installed? */
  107.   if (name && (*name == '*') && !(strchr (name,'/')) &&
  108.       !stat ((char *) mail_parameters (NIL,GET_NEWSSPOOL,NIL),&sbuf) &&
  109.       ((fd = open ((char *) mail_parameters (NIL,GET_NEWSACTIVE,NIL),O_RDONLY,
  110.            NIL)) >= 0)) {
  111.     fstat (fd,&sbuf);        /* get size of active file */
  112.                 /* slurp in active file */
  113.     read (fd,t = s = (char *) fs_get (sbuf.st_size+1),sbuf.st_size);
  114.     s[sbuf.st_size] = '\0';    /* tie off file */
  115.     close (fd);            /* flush file */
  116.     lcase (strcpy (tmp,name+1));/* make sure compare with lower case */
  117.     while (*t && (u = strchr (t,' '))) {
  118.       *u++ = '\0';        /* tie off at end of name */
  119.       if (!strcmp (tmp,t)) {    /* name matches? */
  120.     ret = &newsdriver;    /* seems to be a valid name */
  121.     break;
  122.       }
  123.       t = 1 + strchr (u,'\n');    /* next line */
  124.     }
  125.     fs_give ((void **) &s);    /* flush data */
  126.  }
  127.   return ret;            /* return status */
  128. }
  129.  
  130.  
  131. /* News manipulate driver parameters
  132.  * Accepts: function code
  133.  *        function-dependent value
  134.  * Returns: function-dependent return value
  135.  */
  136.  
  137. void *news_parameters (function,value)
  138.     long function;
  139.     void *value;
  140. {
  141.   return NIL;
  142. }
  143.  
  144. /* Netnews mail find list of mailboxes
  145.  * Accepts: mail stream
  146.  *        pattern to search
  147.  */
  148.  
  149. void news_find (stream,pat)
  150.     MAILSTREAM *stream;
  151.     char *pat;
  152. {
  153.   /* Always a no-op */
  154. }
  155.  
  156.  
  157. /* Netnews mail find list of bboards
  158.  * Accepts: mail stream
  159.  *        pattern to search
  160.  */
  161.  
  162. void news_find_bboards (stream,pat)
  163.     MAILSTREAM *stream;
  164.     char *pat;
  165. {
  166.   char *t;
  167.   void *s = NIL;
  168.   struct stat sbuf;
  169.                 /* read from newsrc if have local news spool */
  170.   if (!(stat ((char *) mail_parameters (NIL,GET_NEWSSPOOL,NIL),&sbuf) ||
  171.     stat ((char *) mail_parameters (NIL,GET_NEWSACTIVE,NIL),&sbuf) ||
  172.     (stream && stream->anonymous))) newsrc_find (pat);
  173.   while (t = sm_read (&s))    /* get data from subscription manager */
  174.     if ((*t == '*') && pmatch (t+1,pat)) mm_bboard (t+1);
  175. }
  176.  
  177. /* Netnews mail find list of all mailboxes
  178.  * Accepts: mail stream
  179.  *        pattern to search
  180.  */
  181.  
  182. void news_find_all (stream,pat)
  183.     MAILSTREAM *stream;
  184.     char *pat;
  185. {
  186.   /* Always a no-op */
  187. }
  188.  
  189.  
  190. /* Netnews mail find list of all bboards
  191.  * Accepts: mail stream
  192.  *        pattern to search
  193.  */
  194.  
  195. void news_find_all_bboards (stream,pat)
  196.     MAILSTREAM *stream;
  197.     char *pat;
  198. {
  199.   int fd;
  200.   char patx[MAILTMPLEN];
  201.   char *s,*t,*u;
  202.   struct stat sbuf;
  203.   lcase (strcpy (patx,pat));    /* make sure compare with lower case */
  204.   if ((!stat ((char *) mail_parameters (NIL,GET_NEWSSPOOL,NIL),&sbuf)) &&
  205.       ((fd = open ((char *) mail_parameters (NIL,GET_NEWSACTIVE,NIL),O_RDONLY,
  206.            NIL)) >= 0)) {
  207.     fstat (fd,&sbuf);        /* get file size and read data */
  208.     read (fd,s = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
  209.     close (fd);            /* close file */
  210.     s[sbuf.st_size] = '\0';    /* tie off string */
  211.     if (t = strtok (s,"\n")) do if (u = strchr (t,' ')) {
  212.       *u = '\0';        /* tie off at end of name */
  213.       if (pmatch (lcase (t),patx)) mm_bboard (t);
  214.     } while (t = strtok (NIL,"\n"));
  215.     fs_give ((void **) &s);
  216.   }
  217. }
  218.  
  219. /* Netnews mail subscribe to mailbox
  220.  * Accepts: mail stream
  221.  *        mailbox to add to subscription list
  222.  * Returns: T on success, NIL on failure
  223.  */
  224.  
  225. long news_subscribe (stream,mailbox)
  226.     MAILSTREAM *stream;
  227.     char *mailbox;
  228. {
  229.   return NIL;            /* never valid for netnews */
  230. }
  231.  
  232.  
  233. /* Netnews mail unsubscribe to mailbox
  234.  * Accepts: mail stream
  235.  *        mailbox to delete from subscription list
  236.  * Returns: T on success, NIL on failure
  237.  */
  238.  
  239. long news_unsubscribe (stream,mailbox)
  240.     MAILSTREAM *stream;
  241.     char *mailbox;
  242. {
  243.   return NIL;            /* never valid for netnews */
  244. }
  245.  
  246.  
  247. /* Netnews mail subscribe to bboard
  248.  * Accepts: mail stream
  249.  *        bboard to add to subscription list
  250.  * Returns: T on success, NIL on failure
  251.  */
  252.  
  253. long news_subscribe_bboard (stream,mailbox)
  254.     MAILSTREAM *stream;
  255.     char *mailbox;
  256. {
  257.   return newsrc_update (mailbox,':');
  258. }
  259.  
  260.  
  261. /* Netnews mail unsubscribe to bboard
  262.  * Accepts: mail stream
  263.  *        bboard to delete from subscription list
  264.  * Returns: T on success, NIL on failure
  265.  */
  266.  
  267. long news_unsubscribe_bboard (stream,mailbox)
  268.     MAILSTREAM *stream;
  269.     char *mailbox;
  270. {
  271.   return newsrc_update (mailbox,'!');
  272. }
  273.  
  274. /* Netnews mail create mailbox
  275.  * Accepts: mail stream
  276.  *        mailbox name to create
  277.  * Returns: T on success, NIL on failure
  278.  */
  279.  
  280. long news_create (stream,mailbox)
  281.     MAILSTREAM *stream;
  282.     char *mailbox;
  283. {
  284.   return NIL;            /* never valid for netnews */
  285. }
  286.  
  287.  
  288. /* Netnews mail delete mailbox
  289.  *        mailbox name to delete
  290.  * Returns: T on success, NIL on failure
  291.  */
  292.  
  293. long news_delete (stream,mailbox)
  294.     MAILSTREAM *stream;
  295.     char *mailbox;
  296. {
  297.   return NIL;            /* never valid for netnews */
  298. }
  299.  
  300.  
  301. /* Netnews mail rename mailbox
  302.  * Accepts: mail stream
  303.  *        old mailbox name
  304.  *        new mailbox name
  305.  * Returns: T on success, NIL on failure
  306.  */
  307.  
  308. long news_rename (stream,old,new)
  309.     MAILSTREAM *stream;
  310.     char *old;
  311.     char *new;
  312. {
  313.   return NIL;            /* never valid for netnews */
  314. }
  315.  
  316. /* Netnews mail open
  317.  * Accepts: stream to open
  318.  * Returns: stream on success, NIL on failure
  319.  */
  320.  
  321. MAILSTREAM *news_open (stream)
  322.     MAILSTREAM *stream;
  323. {
  324.   long i,nmsgs,j,k;
  325.   char c = NIL,*s,*t,tmp[MAILTMPLEN];
  326.   struct direct **names;
  327.                   /* return prototype for OP_PROTOTYPE call */
  328.   if (!stream) return &newsproto;
  329.   if (LOCAL) {            /* close old file if stream being recycled */
  330.     news_close (stream);    /* dump and save the changes */
  331.     stream->dtb = &newsdriver;    /* reattach this driver */
  332.     mail_free_cache (stream);    /* clean up cache */
  333.   }
  334.   lcase (stream->mailbox);    /* build news directory name */
  335.   sprintf (s = tmp,"%s/%s",(char *) mail_parameters (NIL,GET_NEWSSPOOL,NIL),
  336.        stream->mailbox + 1);
  337.   while (s = strchr (s,'.')) *s = '/';
  338.                 /* scan directory */
  339.   if ((nmsgs = scandir (tmp,&names,news_select,news_numsort)) >= 0) {
  340.     stream->local = fs_get (sizeof (NEWSLOCAL));
  341.     LOCAL->dirty = NIL;        /* no update to .newsrc needed yet */
  342.     LOCAL->dir = cpystr (tmp);    /* copy directory name for later */
  343.     LOCAL->name = cpystr (stream->mailbox + 1);
  344.                 /* create cache */
  345.     LOCAL->number = (unsigned long *) fs_get (nmsgs * sizeof (unsigned long));
  346.     LOCAL->header = (char **) fs_get (nmsgs * sizeof (char *));
  347.     LOCAL->body = (char **) fs_get (nmsgs * sizeof (char *));
  348.     for (i = 0; i<nmsgs; ++i) {    /* initialize cache */
  349.       LOCAL->number[i] = atoi (names[i]->d_name);
  350.       fs_give ((void **) &names[i]);
  351.       LOCAL->header[i] = LOCAL->body[i] = NIL;
  352.     }
  353.     fs_give ((void **) &names);    /* free directory */
  354.                 /* make temporary buffer */
  355.     LOCAL->buf = (char *) fs_get ((LOCAL->buflen = MAXMESSAGESIZE) + 1);
  356.     stream->sequence++;        /* bump sequence number */
  357.     stream->rdonly = T;        /* make sure higher level knows readonly */
  358.     mail_exists (stream,nmsgs);    /* notify upper level that messages exist */
  359.                 /* read .newsrc entries */
  360.     mail_recent (stream,newsrc_read (LOCAL->name,stream,LOCAL->number));
  361.                 /* notify if empty bboard */
  362.     if (!(stream->nmsgs || stream->silent)) {
  363.       sprintf (tmp,"Newsgroup %s is empty",LOCAL->name);
  364.       mm_log (tmp,WARN);
  365.     }
  366.   }
  367.   return LOCAL ? stream : NIL;    /* if stream is alive, return to caller */
  368. }
  369.  
  370. /* Netnews file name selection test
  371.  * Accepts: candidate directory entry
  372.  * Returns: T to use file name, NIL to skip it
  373.  */
  374.  
  375. int news_select (name)
  376.     struct direct *name;
  377. {
  378.   char c;
  379.   char *s = name->d_name;
  380.   while (c = *s++) if (!isdigit (c)) return NIL;
  381.   return T;
  382. }
  383.  
  384.  
  385. /* Netnews file name comparision
  386.  * Accepts: first candidate directory entry
  387.  *        second candidate directory entry
  388.  * Returns: negative if d1 < d2, 0 if d1 == d2, postive if d1 > d2
  389.  */
  390.  
  391. int news_numsort (d1,d2)
  392.     struct direct **d1;
  393.     struct direct **d2;
  394. {
  395.   return (atoi ((*d1)->d_name) - atoi ((*d2)->d_name));
  396. }
  397.  
  398.  
  399. /* Netnews mail close
  400.  * Accepts: MAIL stream
  401.  */
  402.  
  403. void news_close (stream)
  404.     MAILSTREAM *stream;
  405. {
  406.   if (LOCAL) {            /* only if a file is open */
  407.     news_check (stream);    /* dump final checkpoint */
  408.     if (LOCAL->dir) fs_give ((void **) &LOCAL->dir);
  409.     if (LOCAL->name) fs_give ((void **) &LOCAL->name);
  410.     news_gc (stream,GC_TEXTS);    /* free local cache */
  411.     fs_give ((void **) &LOCAL->number);
  412.     fs_give ((void **) &LOCAL->header);
  413.     fs_give ((void **) &LOCAL->body);
  414.                 /* free local scratch buffer */
  415.     if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
  416.                 /* nuke the local data */
  417.     fs_give ((void **) &stream->local);
  418.     stream->dtb = NIL;        /* log out the DTB */
  419.   }
  420. }
  421.  
  422. /* Netnews mail fetch fast information
  423.  * Accepts: MAIL stream
  424.  *        sequence
  425.  */
  426.  
  427. void news_fetchfast (stream,sequence)
  428.     MAILSTREAM *stream;
  429.     char *sequence;
  430. {
  431.   long i;
  432.                 /* ugly and slow */
  433.   if (stream && LOCAL && mail_sequence (stream,sequence))
  434.     for (i = 1; i <= stream->nmsgs; i++)
  435.       if (mail_elt (stream,i)->sequence)
  436.     news_fetchheader (stream,i);
  437. }
  438.  
  439.  
  440. /* Netnews mail fetch flags
  441.  * Accepts: MAIL stream
  442.  *        sequence
  443.  */
  444.  
  445. void news_fetchflags (stream,sequence)
  446.     MAILSTREAM *stream;
  447.     char *sequence;
  448. {
  449.   return;            /* no-op for local mail */
  450. }
  451.  
  452.  
  453. /* Netnews mail fetch envelope
  454.  * Accepts: MAIL stream
  455.  *        message # to fetch
  456.  *        pointer to return body
  457.  * Returns: envelope of this message, body returned in body value
  458.  *
  459.  * Fetches the "fast" information as well
  460.  */
  461.  
  462. ENVELOPE *news_fetchstructure (stream,msgno,body)
  463.     MAILSTREAM *stream;
  464.     long msgno;
  465.     BODY **body;
  466. {
  467.   char *h,*t;
  468.   LONGCACHE *lelt;
  469.   ENVELOPE **env;
  470.   STRING bs;
  471.   BODY **b;
  472.   if (stream->scache) {        /* short cache */
  473.     if (msgno != stream->msgno){/* flush old poop if a different message */
  474.       mail_free_envelope (&stream->env);
  475.       mail_free_body (&stream->body);
  476.     }
  477.     stream->msgno = msgno;
  478.     env = &stream->env;        /* get pointers to envelope and body */
  479.     b = &stream->body;
  480.   }
  481.   else {            /* long cache */
  482.     lelt = mail_lelt (stream,msgno);
  483.     env = &lelt->env;        /* get pointers to envelope and body */
  484.     b = &lelt->body;
  485.   }
  486.   if ((body && !*b) || !*env) {    /* have the poop we need? */
  487.     mail_free_envelope (env);    /* flush old envelope and body */
  488.     mail_free_body (b);
  489.     h = news_fetchheader (stream,msgno);
  490.                 /* can't use fetchtext since it'll set seen */
  491.     t = LOCAL->body[msgno - 1] ? LOCAL->body[msgno - 1] : "";
  492.     INIT (&bs,mail_string,(void *) t,strlen (t));
  493.                 /* parse envelope and body */
  494.     rfc822_parse_msg (env,body ? b : NIL,h,strlen (h),&bs,BADHOST,LOCAL->buf);
  495.   }
  496.   if (body) *body = *b;        /* return the body */
  497.   return *env;            /* return the envelope */
  498. }
  499.  
  500. /* Netnews mail fetch message header
  501.  * Accepts: MAIL stream
  502.  *        message # to fetch
  503.  * Returns: message header in RFC822 format
  504.  */
  505.  
  506. char *news_fetchheader (stream,msgno)
  507.     MAILSTREAM *stream;
  508.     long msgno;
  509. {
  510.   unsigned long i,hdrsize;
  511.   int fd;
  512.   char *s,*b;
  513.   long m = msgno - 1;
  514.   struct stat sbuf;
  515.   struct tm *tm;
  516.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  517.                 /* build message file name */
  518.   sprintf (LOCAL->buf,"%s/%lu",LOCAL->dir,LOCAL->number[m]);
  519.   if (!LOCAL->header[m] && ((fd = open (LOCAL->buf,O_RDONLY,NIL)) >= 0)) {
  520.     fstat (fd,&sbuf);        /* get size of message */
  521.                 /* make plausible IMAPish date string */
  522.     tm = gmtime (&sbuf.st_mtime);
  523.     elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
  524.     elt->year = tm->tm_year + 1900 - BASEYEAR;
  525.     elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
  526.     elt->seconds = tm->tm_sec;
  527.     elt->zhours = 0; elt->zminutes = 0;
  528.                 /* slurp message */
  529.     read (fd,s = (char *) fs_get (sbuf.st_size +1),sbuf.st_size);
  530.     s[sbuf.st_size] = '\0';    /* tie off file */
  531.     close (fd);            /* flush message file */
  532.                 /* find end of header */
  533.     for (i = 0,b = s; *b && !(i && (*b == '\n')); i = (*b++ == '\n'));
  534.     hdrsize = (*b ? ++b : b)-s;    /* number of header bytes */
  535.     elt->rfc822_size =        /* size of entire message in CRLF form */
  536.       strcrlfcpy (&LOCAL->header[m],&i,s,hdrsize) +
  537.     strcrlfcpy (&LOCAL->body[m],&i,b,sbuf.st_size - hdrsize);
  538.     fs_give ((void **) &s);    /* flush old data */
  539.   }
  540.   return LOCAL->header[m] ? LOCAL->header[m] : "";
  541. }
  542.  
  543. /* Netnews mail fetch message text (only)
  544.     body only;
  545.  * Accepts: MAIL stream
  546.  *        message # to fetch
  547.  * Returns: message text in RFC822 format
  548.  */
  549.  
  550. char *news_fetchtext (stream,msgno)
  551.     MAILSTREAM *stream;
  552.     long msgno;
  553. {
  554.   long i = msgno - 1;
  555.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  556.                 /* snarf message in case don't have it yet */
  557.   news_fetchheader (stream,msgno);
  558.   elt->seen = T;        /* mark as seen */
  559.   return LOCAL->body[i] ? LOCAL->body[i] : "";
  560. }
  561.  
  562. /* Netnews fetch message body as a structure
  563.  * Accepts: Mail stream
  564.  *        message # to fetch
  565.  *        section specifier
  566.  *        pointer to length
  567.  * Returns: pointer to section of message body
  568.  */
  569.  
  570. char *news_fetchbody (stream,m,s,len)
  571.     MAILSTREAM *stream;
  572.     long m;
  573.     char *s;
  574.     unsigned long *len;
  575. {
  576.   BODY *b;
  577.   PART *pt;
  578.   unsigned long i;
  579.   char *base;
  580.   unsigned long offset = 0;
  581.   MESSAGECACHE *elt = mail_elt (stream,m);
  582.                 /* make sure have a body */
  583.   if (!(news_fetchstructure (stream,m,&b) && b && s && *s &&
  584.     ((i = strtol (s,&s,10)) > 0) && (base = news_fetchtext (stream,m))))
  585.     return NIL;
  586.   do {                /* until find desired body part */
  587.                 /* multipart content? */
  588.     if (b->type == TYPEMULTIPART) {
  589.       pt = b->contents.part;    /* yes, find desired part */
  590.       while (--i && (pt = pt->next));
  591.       if (!pt) return NIL;    /* bad specifier */
  592.                 /* note new body, check valid nesting */
  593.       if (((b = &pt->body)->type == TYPEMULTIPART) && !*s) return NIL;
  594.       offset = pt->offset;    /* get new offset */
  595.     }
  596.     else if (i != 1) return NIL;/* otherwise must be section 1 */
  597.                 /* need to go down further? */
  598.     if (i = *s) switch (b->type) {
  599.     case TYPEMESSAGE:        /* embedded message, calculate new base */
  600.       offset = b->contents.msg.offset;
  601.       b = b->contents.msg.body;    /* get its body, drop into multipart case */
  602.     case TYPEMULTIPART:        /* multipart, get next section */
  603.       if ((*s++ == '.') && (i = strtol (s,&s,10)) > 0) break;
  604.     default:            /* bogus subpart specification */
  605.       return NIL;
  606.     }
  607.   } while (i);
  608.                 /* lose if body bogus */
  609.   if ((!b) || b->type == TYPEMULTIPART) return NIL;
  610.   elt->seen = T;        /* mark as seen */
  611.   return rfc822_contents (&LOCAL->buf,&LOCAL->buflen,len,base + offset,
  612.               b->size.ibytes,b->encoding);
  613. }
  614.  
  615. /* Netnews mail set flag
  616.  * Accepts: MAIL stream
  617.  *        sequence
  618.  *        flag(s)
  619.  */
  620.  
  621. void news_setflag (stream,sequence,flag)
  622.     MAILSTREAM *stream;
  623.     char *sequence;
  624.     char *flag;
  625. {
  626.   MESSAGECACHE *elt;
  627.   long i;
  628.   short f = news_getflags (stream,flag);
  629.   if (!f) return;        /* no-op if no flags to modify */
  630.                 /* get sequence and loop on it */
  631.   if (mail_sequence (stream,sequence)) for (i = 0; i < stream->nmsgs; i++)
  632.     if ((elt = mail_elt (stream,i + 1))->sequence) {
  633.       if (f&fSEEN) elt->seen=T;    /* set all requested flags */
  634.       if (f&fDELETED) {        /* deletion also purges the cache */
  635.     elt->deleted = T;    /* mark deleted */
  636.     LOCAL->dirty = T;    /* mark dirty */
  637.     if (LOCAL->header[i]) fs_give ((void **) &LOCAL->header[i]);
  638.     if (LOCAL->body[i]) fs_give ((void **) &LOCAL->body[i]);
  639.       }
  640.       if (f&fFLAGGED) elt->flagged = T;
  641.       if (f&fANSWERED) elt->answered = T;
  642.     }
  643. }
  644.  
  645.  
  646. /* Netnews mail clear flag
  647.  * Accepts: MAIL stream
  648.  *        sequence
  649.  *        flag(s)
  650.  */
  651.  
  652. void news_clearflag (stream,sequence,flag)
  653.     MAILSTREAM *stream;
  654.     char *sequence;
  655.     char *flag;
  656. {
  657.   MESSAGECACHE *elt;
  658.   long i;
  659.   short f = news_getflags (stream,flag);
  660.   if (!f) return;        /* no-op if no flags to modify */
  661.                 /* get sequence and loop on it */
  662.   if (mail_sequence (stream,sequence)) for (i = 0; i < stream->nmsgs; i++)
  663.     if ((elt = mail_elt (stream,i + 1))->sequence) {
  664.                 /* clear all requested flags */
  665.       if (f&fSEEN) elt->seen = NIL;
  666.       if (f&fDELETED) {
  667.     elt->deleted = NIL;    /* undelete */
  668.     LOCAL->dirty = T;    /* mark stream as dirty */
  669.       }
  670.       if (f&fFLAGGED) elt->flagged = NIL;
  671.       if (f&fANSWERED) elt->answered = NIL;
  672.     }
  673. }
  674.  
  675. /* Netnews mail search for messages
  676.  * Accepts: MAIL stream
  677.  *        search criteria
  678.  */
  679.  
  680. void news_search (stream,criteria)
  681.     MAILSTREAM *stream;
  682.     char *criteria;
  683. {
  684.   long i,n;
  685.   char *d;
  686.   search_t f;
  687.                 /* initially all searched */
  688.   for (i = 1; i <= stream->nmsgs; ++i) mail_elt (stream,i)->searched = T;
  689.                 /* get first criterion */
  690.   if (criteria && (criteria = strtok (criteria," "))) {
  691.                 /* for each criterion */
  692.     for (; criteria; (criteria = strtok (NIL," "))) {
  693.       f = NIL; d = NIL; n = 0;    /* init then scan the criterion */
  694.       switch (*ucase (criteria)) {
  695.       case 'A':            /* possible ALL, ANSWERED */
  696.     if (!strcmp (criteria+1,"LL")) f = news_search_all;
  697.     else if (!strcmp (criteria+1,"NSWERED")) f = news_search_answered;
  698.     break;
  699.       case 'B':            /* possible BCC, BEFORE, BODY */
  700.     if (!strcmp (criteria+1,"CC"))
  701.       f = news_search_string (news_search_bcc,&d,&n);
  702.     else if (!strcmp (criteria+1,"EFORE"))
  703.       f = news_search_date (news_search_before,&n);
  704.     else if (!strcmp (criteria+1,"ODY"))
  705.       f = news_search_string (news_search_body,&d,&n);
  706.     break;
  707.       case 'C':            /* possible CC */
  708.     if (!strcmp (criteria+1,"C"))
  709.       f = news_search_string (news_search_cc,&d,&n);
  710.     break;
  711.       case 'D':            /* possible DELETED */
  712.     if (!strcmp (criteria+1,"ELETED")) f = news_search_deleted;
  713.     break;
  714.       case 'F':            /* possible FLAGGED, FROM */
  715.     if (!strcmp (criteria+1,"LAGGED")) f = news_search_flagged;
  716.     else if (!strcmp (criteria+1,"ROM"))
  717.       f = news_search_string (news_search_from,&d,&n);
  718.     break;
  719.       case 'K':            /* possible KEYWORD */
  720.     if (!strcmp (criteria+1,"EYWORD"))
  721.       f = news_search_flag (news_search_keyword,&d);
  722.     break;
  723.       case 'N':            /* possible NEW */
  724.     if (!strcmp (criteria+1,"EW")) f = news_search_new;
  725.     break;
  726.  
  727.       case 'O':            /* possible OLD, ON */
  728.     if (!strcmp (criteria+1,"LD")) f = news_search_old;
  729.     else if (!strcmp (criteria+1,"N"))
  730.       f = news_search_date (news_search_on,&n);
  731.     break;
  732.       case 'R':            /* possible RECENT */
  733.     if (!strcmp (criteria+1,"ECENT")) f = news_search_recent;
  734.     break;
  735.       case 'S':            /* possible SEEN, SINCE, SUBJECT */
  736.     if (!strcmp (criteria+1,"EEN")) f = news_search_seen;
  737.     else if (!strcmp (criteria+1,"INCE"))
  738.       f = news_search_date (news_search_since,&n);
  739.     else if (!strcmp (criteria+1,"UBJECT"))
  740.       f = news_search_string (news_search_subject,&d,&n);
  741.     break;
  742.       case 'T':            /* possible TEXT, TO */
  743.     if (!strcmp (criteria+1,"EXT"))
  744.       f = news_search_string (news_search_text,&d,&n);
  745.     else if (!strcmp (criteria+1,"O"))
  746.       f = news_search_string (news_search_to,&d,&n);
  747.     break;
  748.       case 'U':            /* possible UN* */
  749.     if (criteria[1] == 'N') {
  750.       if (!strcmp (criteria+2,"ANSWERED")) f = news_search_unanswered;
  751.       else if (!strcmp (criteria+2,"DELETED")) f = news_search_undeleted;
  752.       else if (!strcmp (criteria+2,"FLAGGED")) f = news_search_unflagged;
  753.       else if (!strcmp (criteria+2,"KEYWORD"))
  754.         f = news_search_flag (news_search_unkeyword,&d);
  755.       else if (!strcmp (criteria+2,"SEEN")) f = news_search_unseen;
  756.     }
  757.     break;
  758.       default:            /* we will barf below */
  759.     break;
  760.       }
  761.       if (!f) {            /* if can't determine any criteria */
  762.     sprintf (LOCAL->buf,"Unknown search criterion: %.80s",criteria);
  763.     mm_log (LOCAL->buf,ERROR);
  764.     return;
  765.       }
  766.                 /* run the search criterion */
  767.       for (i = 1; i <= stream->nmsgs; ++i)
  768.     if (mail_elt (stream,i)->searched && !(*f) (stream,i,d,n))
  769.       mail_elt (stream,i)->searched = NIL;
  770.     }
  771.                 /* report search results to main program */
  772.     for (i = 1; i <= stream->nmsgs; ++i)
  773.       if (mail_elt (stream,i)->searched) mail_searched (stream,i);
  774.   }
  775. }
  776.  
  777. /* Netnews mail ping mailbox
  778.  * Accepts: MAIL stream
  779.  * Returns: T if stream alive, else NIL
  780.  */
  781.  
  782. long news_ping (stream)
  783.     MAILSTREAM *stream;
  784. {
  785.   return T;            /* always alive */
  786. }
  787.  
  788.  
  789. /* Netnews mail check mailbox
  790.  * Accepts: MAIL stream
  791.  */
  792.  
  793. void news_check (stream)
  794.     MAILSTREAM *stream;
  795. {
  796.                 /* never do if no updates */
  797.   if (LOCAL->dirty) newsrc_write (LOCAL->name,stream,LOCAL->number);
  798. }
  799.  
  800.  
  801. /* Netnews mail expunge mailbox
  802.  * Accepts: MAIL stream
  803.  */
  804.  
  805. void news_expunge (stream)
  806.     MAILSTREAM *stream;
  807. {
  808.   if (!stream->silent) mm_log ("Expunge ignored on readonly mailbox",NIL);
  809. }
  810.  
  811. /* Netnews mail copy message(s)
  812.     s;
  813.  * Accepts: MAIL stream
  814.  *        sequence
  815.  *        destination mailbox
  816.  * Returns: T if copy successful, else NIL
  817.  */
  818.  
  819. long news_copy (stream,sequence,mailbox)
  820.     MAILSTREAM *stream;
  821.     char *sequence;
  822.     char *mailbox;
  823. {
  824.   mm_log ("Copy not valid for netnews",ERROR);
  825.   return NIL;
  826. }
  827.  
  828.  
  829. /* Netnews mail move message(s)
  830.     s;
  831.  * Accepts: MAIL stream
  832.  *        sequence
  833.  *        destination mailbox
  834.  * Returns: T if move successful, else NIL
  835.  */
  836.  
  837. long news_move (stream,sequence,mailbox)
  838.     MAILSTREAM *stream;
  839.     char *sequence;
  840.     char *mailbox;
  841. {
  842.   mm_log ("Move not valid for netnews",ERROR);
  843.   return NIL;
  844. }
  845.  
  846.  
  847. /* Netnews mail append message from stringstruct
  848.  * Accepts: MAIL stream
  849.  *        destination mailbox
  850.  *        stringstruct of messages to append
  851.  * Returns: T if append successful, else NIL
  852.  */
  853.  
  854. long news_append (stream,mailbox,flags,date,message)
  855.     MAILSTREAM *stream;
  856.     char *mailbox;
  857.     char *flags;
  858.     char *date;
  859.                STRING *message;
  860. {
  861.   mm_log ("Append not valid for netnews",ERROR);
  862.   return NIL;
  863. }
  864.  
  865.  
  866. /* Netnews garbage collect stream
  867.  * Accepts: Mail stream
  868.  *        garbage collection flags
  869.  */
  870.  
  871. void news_gc (stream,gcflags)
  872.     MAILSTREAM *stream;
  873.     long gcflags;
  874. {
  875.   unsigned long i;
  876.   if (gcflags & GC_TEXTS)    /* garbage collect texts? */
  877.                 /* flush texts from cache */
  878.     for (i = 0; i < stream->nmsgs; i++) {
  879.       if (LOCAL->header[i]) fs_give ((void **) &LOCAL->header[i]);
  880.       if (LOCAL->body[i]) fs_give ((void **) &LOCAL->body[i]);
  881.     }
  882. }
  883.  
  884. /* Internal routines */
  885.  
  886.  
  887. /* Parse flag list
  888.  * Accepts: MAIL stream
  889.  *        flag list as a character string
  890.  * Returns: flag command list
  891.  */
  892.  
  893. short news_getflags (stream,flag)
  894.     MAILSTREAM *stream;
  895.     char *flag;
  896. {
  897.   char *t,tmp[MAILTMPLEN],err[MAILTMPLEN];
  898.   short f = 0;
  899.   short i,j;
  900.   if (flag && *flag) {        /* no-op if no flag string */
  901.                 /* check if a list and make sure valid */
  902.     if ((i = (*flag == '(')) ^ (flag[strlen (flag)-1] == ')')) {
  903.       mm_log ("Bad flag list",ERROR);
  904.       return NIL;
  905.     }
  906.                 /* copy the flag string w/o list construct */
  907.     strncpy (tmp,flag+i,(j = strlen (flag) - (2*i)));
  908.     tmp[j] = '\0';
  909.     t = ucase (tmp);        /* uppercase only from now on */
  910.  
  911.     while (t && *t) {        /* parse the flags */
  912.       if (*t == '\\') {        /* system flag? */
  913.     switch (*++t) {        /* dispatch based on first character */
  914.     case 'S':        /* possible \Seen flag */
  915.       if (t[1] == 'E' && t[2] == 'E' && t[3] == 'N') i = fSEEN;
  916.       t += 4;        /* skip past flag name */
  917.       break;
  918.     case 'D':        /* possible \Deleted flag */
  919.       if (t[1] == 'E' && t[2] == 'L' && t[3] == 'E' && t[4] == 'T' &&
  920.           t[5] == 'E' && t[6] == 'D') i = fDELETED;
  921.       t += 7;        /* skip past flag name */
  922.       break;
  923.     case 'F':        /* possible \Flagged flag */
  924.       if (t[1] == 'L' && t[2] == 'A' && t[3] == 'G' && t[4] == 'G' &&
  925.           t[5] == 'E' && t[6] == 'D') i = fFLAGGED;
  926.       t += 7;        /* skip past flag name */
  927.       break;
  928.     case 'A':        /* possible \Answered flag */
  929.       if (t[1] == 'N' && t[2] == 'S' && t[3] == 'W' && t[4] == 'E' &&
  930.           t[5] == 'R' && t[6] == 'E' && t[7] == 'D') i = fANSWERED;
  931.       t += 8;        /* skip past flag name */
  932.       break;
  933.     default:        /* unknown */
  934.       i = 0;
  935.       break;
  936.     }
  937.                 /* add flag to flags list */
  938.     if (i && ((*t == '\0') || (*t++ == ' '))) f |= i;
  939.       }
  940.       else {            /* no user flags yet */
  941.     t = strtok (t," ");    /* isolate flag name */
  942.     sprintf (err,"Unknown flag: %.80s",t);
  943.     t = strtok (NIL," ");    /* get next flag */
  944.     mm_log (err,ERROR);
  945.       }
  946.     }
  947.   }
  948.   return f;
  949. }
  950.  
  951. /* Search support routines
  952.  * Accepts: MAIL stream
  953.  *        message number
  954.  *        pointer to additional data
  955.  *        pointer to temporary buffer
  956.  * Returns: T if search matches, else NIL
  957.  */
  958.  
  959. char news_search_all (stream,msgno,d,n)
  960.     MAILSTREAM *stream;
  961.     long msgno;
  962.     char *d;
  963.     long n;
  964. {
  965.   return T;            /* ALL always succeeds */
  966. }
  967.  
  968.  
  969. char news_search_answered (stream,msgno,d,n)
  970.     MAILSTREAM *stream;
  971.     long msgno;
  972.     char *d;
  973.     long n;
  974. {
  975.   return mail_elt (stream,msgno)->answered ? T : NIL;
  976. }
  977.  
  978.  
  979. char news_search_deleted (stream,msgno,d,n)
  980.     MAILSTREAM *stream;
  981.     long msgno;
  982.     char *d;
  983.     long n;
  984. {
  985.   return mail_elt (stream,msgno)->deleted ? T : NIL;
  986. }
  987.  
  988.  
  989. char news_search_flagged (stream,msgno,d,n)
  990.     MAILSTREAM *stream;
  991.     long msgno;
  992.     char *d;
  993.     long n;
  994. {
  995.   return mail_elt (stream,msgno)->flagged ? T : NIL;
  996. }
  997.  
  998.  
  999. char news_search_keyword (stream,msgno,d,n)
  1000.     MAILSTREAM *stream;
  1001.     long msgno;
  1002.     char *d;
  1003.     long n;
  1004. {
  1005.   return NIL;            /* keywords not supported yet */
  1006. }
  1007.  
  1008.  
  1009. char news_search_new (stream,msgno,d,n)
  1010.     MAILSTREAM *stream;
  1011.     long msgno;
  1012.     char *d;
  1013.     long n;
  1014. {
  1015.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  1016.   return (elt->recent && !elt->seen) ? T : NIL;
  1017. }
  1018.  
  1019. char news_search_old (stream,msgno,d,n)
  1020.     MAILSTREAM *stream;
  1021.     long msgno;
  1022.     char *d;
  1023.     long n;
  1024. {
  1025.   return mail_elt (stream,msgno)->recent ? NIL : T;
  1026. }
  1027.  
  1028.  
  1029. char news_search_recent (stream,msgno,d,n)
  1030.     MAILSTREAM *stream;
  1031.     long msgno;
  1032.     char *d;
  1033.     long n;
  1034. {
  1035.   return mail_elt (stream,msgno)->recent ? T : NIL;
  1036. }
  1037.  
  1038.  
  1039. char news_search_seen (stream,msgno,d,n)
  1040.     MAILSTREAM *stream;
  1041.     long msgno;
  1042.     char *d;
  1043.     long n;
  1044. {
  1045.   return mail_elt (stream,msgno)->seen ? T : NIL;
  1046. }
  1047.  
  1048.  
  1049. char news_search_unanswered (stream,msgno,d,n)
  1050.     MAILSTREAM *stream;
  1051.     long msgno;
  1052.     char *d;
  1053.     long n;
  1054. {
  1055.   return mail_elt (stream,msgno)->answered ? NIL : T;
  1056. }
  1057.  
  1058.  
  1059. char news_search_undeleted (stream,msgno,d,n)
  1060.     MAILSTREAM *stream;
  1061.     long msgno;
  1062.     char *d;
  1063.     long n;
  1064. {
  1065.   return mail_elt (stream,msgno)->deleted ? NIL : T;
  1066. }
  1067.  
  1068.  
  1069. char news_search_unflagged (stream,msgno,d,n)
  1070.     MAILSTREAM *stream;
  1071.     long msgno;
  1072.     char *d;
  1073.     long n;
  1074. {
  1075.   return mail_elt (stream,msgno)->flagged ? NIL : T;
  1076. }
  1077.  
  1078.  
  1079. char news_search_unkeyword (stream,msgno,d,n)
  1080.     MAILSTREAM *stream;
  1081.     long msgno;
  1082.     char *d;
  1083.     long n;
  1084. {
  1085.   return T;            /* keywords not supported yet */
  1086. }
  1087.  
  1088.  
  1089. char news_search_unseen (stream,msgno,d,n)
  1090.     MAILSTREAM *stream;
  1091.     long msgno;
  1092.     char *d;
  1093.     long n;
  1094. {
  1095.   return mail_elt (stream,msgno)->seen ? NIL : T;
  1096. }
  1097.  
  1098. char news_search_before (stream,msgno,d,n)
  1099.     MAILSTREAM *stream;
  1100.     long msgno;
  1101.     char *d;
  1102.     long n;
  1103. {
  1104.   return (char) (news_msgdate (stream,msgno) < n);
  1105. }
  1106.  
  1107.  
  1108. char news_search_on (stream,msgno,d,n)
  1109.     MAILSTREAM *stream;
  1110.     long msgno;
  1111.     char *d;
  1112.     long n;
  1113. {
  1114.   return (char) (news_msgdate (stream,msgno) == n);
  1115. }
  1116.  
  1117.  
  1118. char news_search_since (stream,msgno,d,n)
  1119.     MAILSTREAM *stream;
  1120.     long msgno;
  1121.     char *d;
  1122.     long n;
  1123. {
  1124.                 /* everybody interprets "since" as .GE. */
  1125.   return (char) (news_msgdate (stream,msgno) >= n);
  1126. }
  1127.  
  1128.  
  1129. unsigned long news_msgdate (stream,msgno)
  1130.     MAILSTREAM *stream;
  1131.     long msgno;
  1132. {
  1133.   struct stat sbuf;
  1134.   struct tm *tm;
  1135.   MESSAGECACHE *elt = mail_elt (stream,msgno);
  1136.   if (!elt->day) {        /* get date if don't have it yet */
  1137.                 /* build message file name */
  1138.     sprintf (LOCAL->buf,"%s/%lu",LOCAL->dir,LOCAL->number[msgno - 1]);
  1139.     stat (LOCAL->buf,&sbuf);    /* get message date */
  1140.     tm = gmtime (&sbuf.st_mtime);
  1141.     elt->day = tm->tm_mday; elt->month = tm->tm_mon + 1;
  1142.     elt->year = tm->tm_year + 1900 - BASEYEAR;
  1143.     elt->hours = tm->tm_hour; elt->minutes = tm->tm_min;
  1144.     elt->seconds = tm->tm_sec;
  1145.     elt->zhours = 0; elt->zminutes = 0;
  1146.   }
  1147.   return (long) (elt->year << 9) + (elt->month << 5) + elt->day;
  1148. }
  1149.  
  1150.  
  1151. char news_search_body (stream,msgno,d,n)
  1152.     MAILSTREAM *stream;
  1153.     long msgno;
  1154.     char *d;
  1155.     long n;
  1156. {
  1157.   long i = msgno - 1;
  1158.   news_fetchheader (stream,msgno);
  1159.   return LOCAL->body[i] ?
  1160.     search (LOCAL->body[i],strlen (LOCAL->body[i]),d,n) : NIL;
  1161. }
  1162.  
  1163.  
  1164. char news_search_subject (stream,msgno,d,n)
  1165.     MAILSTREAM *stream;
  1166.     long msgno;
  1167.     char *d;
  1168.     long n;
  1169. {
  1170.   char *t = news_fetchstructure (stream,msgno,NIL)->subject;
  1171.   return t ? search (t,strlen (t),d,n) : NIL;
  1172. }
  1173.  
  1174.  
  1175. char news_search_text (stream,msgno,d,n)
  1176.     MAILSTREAM *stream;
  1177.     long msgno;
  1178.     char *d;
  1179.     long n;
  1180. {
  1181.   char *t = news_fetchheader (stream,msgno);
  1182.   return (t && search (t,strlen (t),d,n)) ||
  1183.     news_search_body (stream,msgno,d,n);
  1184. }
  1185.  
  1186. char news_search_bcc (stream,msgno,d,n)
  1187.     MAILSTREAM *stream;
  1188.     long msgno;
  1189.     char *d;
  1190.     long n;
  1191. {
  1192.   ADDRESS *a = news_fetchstructure (stream,msgno,NIL)->bcc;
  1193.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1194.                 /* get text for address */
  1195.   rfc822_write_address (LOCAL->buf,a);
  1196.   return search (LOCAL->buf,(long) strlen (LOCAL->buf),d,n);
  1197. }
  1198.  
  1199.  
  1200. char news_search_cc (stream,msgno,d,n)
  1201.     MAILSTREAM *stream;
  1202.     long msgno;
  1203.     char *d;
  1204.     long n;
  1205. {
  1206.   ADDRESS *a = news_fetchstructure (stream,msgno,NIL)->cc;
  1207.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1208.                 /* get text for address */
  1209.   rfc822_write_address (LOCAL->buf,a);
  1210.   return search (LOCAL->buf,(long) strlen (LOCAL->buf),d,n);
  1211. }
  1212.  
  1213.  
  1214. char news_search_from (stream,msgno,d,n)
  1215.     MAILSTREAM *stream;
  1216.     long msgno;
  1217.     char *d;
  1218.     long n;
  1219. {
  1220.   ADDRESS *a = news_fetchstructure (stream,msgno,NIL)->from;
  1221.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1222.                 /* get text for address */
  1223.   rfc822_write_address (LOCAL->buf,a);
  1224.   return search (LOCAL->buf,(long) strlen (LOCAL->buf),d,n);
  1225. }
  1226.  
  1227.  
  1228. char news_search_to (stream,msgno,d,n)
  1229.     MAILSTREAM *stream;
  1230.     long msgno;
  1231.     char *d;
  1232.     long n;
  1233. {
  1234.   ADDRESS *a = news_fetchstructure (stream,msgno,NIL)->to;
  1235.   LOCAL->buf[0] = '\0';        /* initially empty string */
  1236.                 /* get text for address */
  1237.   rfc822_write_address (LOCAL->buf,a);
  1238.   return search (LOCAL->buf,(long) strlen (LOCAL->buf),d,n);
  1239. }
  1240.  
  1241. /* Search parsers */
  1242.  
  1243.  
  1244. /* Parse a date
  1245.  * Accepts: function to return
  1246.  *        pointer to date integer to return
  1247.  * Returns: function to return
  1248.  */
  1249.  
  1250. search_t news_search_date (f,n)
  1251.     search_t f;
  1252.     long *n;
  1253. {
  1254.   long i;
  1255.   char *s;
  1256.   MESSAGECACHE elt;
  1257.                 /* parse the date and return fn if OK */
  1258.   return (news_search_string (f,&s,&i) && mail_parse_date (&elt,s) &&
  1259.       (*n = (elt.year << 9) + (elt.month << 5) + elt.day)) ? f : NIL;
  1260. }
  1261.  
  1262. /* Parse a flag
  1263.  * Accepts: function to return
  1264.  *        pointer to string to return
  1265.  * Returns: function to return
  1266.  */
  1267.  
  1268. search_t news_search_flag (f,d)
  1269.     search_t f;
  1270.     char **d;
  1271. {
  1272.                 /* get a keyword, return if OK */
  1273.   return (*d = strtok (NIL," ")) ? f : NIL;
  1274. }
  1275.  
  1276.  
  1277. /* Parse a string
  1278.  * Accepts: function to return
  1279.  *        pointer to string to return
  1280.  *        pointer to string length to return
  1281.  * Returns: function to return
  1282.  */
  1283.  
  1284. search_t news_search_string (f,d,n)
  1285.     search_t f;
  1286.     char **d;
  1287.     long *n;
  1288. {
  1289.   char *end = " ";
  1290.   char *c = strtok (NIL,"");    /* remainder of criteria */
  1291.   if (!c) return NIL;        /* missing argument */
  1292.   switch (*c) {            /* see what the argument is */
  1293.   case '{':            /* literal string */
  1294.     *n = strtol (c+1,d,10);    /* get its length */
  1295.     if ((*(*d)++ == '}') && (*(*d)++ == '\015') && (*(*d)++ == '\012') &&
  1296.     (!(*(c = *d + *n)) || (*c == ' '))) {
  1297.       char e = *--c;
  1298.       *c = DELIM;        /* make sure not a space */
  1299.       strtok (c," ");        /* reset the strtok mechanism */
  1300.       *c = e;            /* put character back */
  1301.       break;
  1302.     }
  1303.   case '\0':            /* catch bogons */
  1304.   case ' ':
  1305.     return NIL;
  1306.   case '"':            /* quoted string */
  1307.     if (strchr (c+1,'"')) end = "\"";
  1308.     else return NIL;
  1309.   default:            /* atomic string */
  1310.     if (*d = strtok (c,end)) *n = strlen (*d);
  1311.     else return NIL;
  1312.     break;
  1313.   }
  1314.   return f;
  1315. }
  1316.